package com.java.base.unsafe;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.PriorityQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.locks.LockSupport;

public class UnsafeTest {

    static Unsafe unsafe;

    static {
        unsafe = getUnsafe();
    }

    public static void main(String[] args) throws NoSuchFieldException {
        test1();
    }

    public static void test1() {
        LockSupport.unpark(Thread.currentThread());
        unsafe.park(false, 3 * 1000000000L);
    }

    public static void test() throws NoSuchFieldException {
        User user = new User();
        user.age = 0;
        long offset = unsafe.objectFieldOffset(User.class.getDeclaredField("age"));
        unsafe.getAndSetInt(user, offset, 18);
        System.out.println(user.age);
    }


    public static class User {
        private int age;
    }

    public static Unsafe getUnsafe() {
        Field field = null;
        try {
            field = Unsafe.class.getDeclaredField("theUnsafe");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        assert field != null;
        field.setAccessible(true);
        try {
            return (Unsafe) field.get(null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return Unsafe.getUnsafe();
    }

}
